package model

type coinLikeModel struct {}

var CoinLikeModel = new(coinLikeModel)

func (m *coinLikeModel) SearchUserLikedCoins(uid int) ([]*CoinLike, error) {
	coinLikes := make([]*CoinLike, 0)
	err := MainDB.Where("uid=?", uid).Find(&coinLikes)
	if err != nil {
		return nil, err
	}
	return coinLikes, nil
}

// 获取用户关注的币
func (m *coinLikeModel) CheckHasUserLiked(uid, coinId int) (bool, error) {
	var coinLike CoinLike
	has, err := MainDB.Where("uid=? AND coin_id=?", uid, coinId).Exist(&coinLike)
	return has, err
}

// 添加关注的币种
func (m *coinLikeModel) AddItem(like *CoinLike) (bool, error) {
	item, err := MainDB.InsertOne(like)
	return item > 0, err
}

// 删除记录
func (m *coinLikeModel) DelItem(uid, coinId int) (bool, error) {
	bean := CoinLike{}
	items, err := MainDB.Where("uid=? AND coin_id=?", uid, coinId).Delete(&bean)
	if err != nil {
		return false, err
	}
	return items > 0, err
}